#pragma rtGlobals=1		// Use modern global access method.

#include <File Name Utilities>

// 
// -------------------------------------------------------------------------
// 
// "JEG_BatchMovie" --
// 
//  Takes a list of ";" separated waveNames and prompts for the first
//  file in a batch of similarly named files (assumes they're named something 
//  like 'base123.ext'). Loads data from all files with that file number 
//  (e.g., 123) or greater and concatenates their results into the waves as 
//  given in waveNames. If more columns are loaded than have supplied names,
//  their results are lost.
// -------------------------------------------------------------------------
// 
Function JEG_BatchMovie()
	
	String fileType="????"
	
	Variable movieFile
	Open/D/R/T=fileType/M="Select the first data file in the movie:" movieFile
	
	if (strlen(S_fileName))		// else user cancelled
	
		// break down the file name into its components
		// assumes it's named something like 'base.123'
		String path = FilePathOnly(S_fileName)
		String file = FileNameOnly(S_fileName)
		String ext = FileExtension(S_fileName)
		
		// clip the leading '.'
		Variable num = str2num(ext[1,inf])
				
		// Just to get things set up		
		
		NewMovie
		
		String loadFile
		Variable tempCount, waveCount = 0
		do
			sprintf loadFile, "%s%s.%03u", path, file, num
			
			Variable ref
			
			Open/R/M="Is there another file?"/Z ref as loadFile
			
			if (V_flag != 0) // file didn't exist
				break
			else
				Close ref
			endif
			
			LoadWave/Q/J/D/W/N/O/K=0 loadFile
			
			DoUpdate
			
			AddMovieFrame
			
			num += 4
		while (num < 100)
		
		CloseMovie
		
		PlayMovie
	endif
End
